package cn.bingoogolapple.bgabanner.demo.ui;

import cn.bingoogolapple.bgabanner.BGABanner;
import cn.bingoogolapple.bgabanner.demo.App;
import cn.bingoogolapple.bgabanner.demo.ResourceTable;
import cn.bingoogolapple.bgabanner.transformer.TransitionEffect;
import ohos.aafwk.ability.Ability;
import ohos.aafwk.content.Intent;
import ohos.aafwk.content.Operation;
import ohos.agp.components.Component;
import ohos.agp.components.Image;
import ohos.agp.components.Text;
import ohos.agp.window.dialog.ToastDialog;

import java.util.ArrayList;

public class MainAbility extends Ability implements BGABanner.Delegate<Image, Object>, BGABanner.Adapter<Image, Object>, Component.ClickedListener {
    private BGABanner mDefaultBanner;
    private BGABanner mCubeBanner;
    private BGABanner mAccordionBanner;
    private BGABanner mFlipBanner;
    private BGABanner mRotateBanner;
    private BGABanner mAlphaBanner;
    private BGABanner mZoomFadeBanner;
    private BGABanner mFadeBanner;
    private BGABanner mZoomCenterBanner;
    private BGABanner mZoomBanner;
    private BGABanner mStackBanner;
    private BGABanner mZoomStackBanner;
    private BGABanner mDepthBanner;
    private Text tv_main_listview_demo;
    private Text tv_main_recylerview_demo;
    private Text tv_main_fresco;
    private Text tv_main_visible;
    private Text tv_main_invisible;
    private Text tv_main_gone;
    private Text tv_main_change_height;
    private Text tv_main_enable_auto_play;
    private Text tv_main_disable_auto_play;
    private Text tv_main_start_auto_play;
    private Text tv_main_stop_auto_play;
    private Text tv_main_get_item_count;
    private Component tv_main_get_current_item;
    private Component tv_main_load_zero_item;
    private Component tv_main_load_one_item;
    private Component tv_main_load_two_item;
    private Component tv_main_load_three_item;
    private Component tv_main_load_five_item;
    private Component tv_main_select_page_one;
    private Component tv_main_select_page_two;
    private Component tv_main_select_page_three;
    private Component tv_main_select_page_four;
    private Component tv_main_select_page_five;
    private Component tv_main_cube;
    private Component tv_main_depth;
    private Component tv_main_flip;
    private Component tv_main_rotate;
    private Component tv_main_alpha;
    private Intent resultData;
    private Operation operation;
    private BGABanner.Adapter mAdapter;

    @Override
    public void onStart(Intent intent) {
        super.onStart(intent);
        initView();
        setListener();
        loadData();
    }

    private void initView() {
        super.setUIContent(ResourceTable.Layout_ability_main);
        mDefaultBanner = (BGABanner) findComponentById(ResourceTable.Id_banner_main_default);
        mCubeBanner = (BGABanner) findComponentById(ResourceTable.Id_banner_main_cube);
        mAccordionBanner = (BGABanner) findComponentById(ResourceTable.Id_banner_main_accordion);
        mAccordionBanner.setIconYesElement(ResourceTable.Media_diamond_select_yes);
        mAccordionBanner.setIconNoElement(ResourceTable.Media_diamond_select_no);
        mFlipBanner = (BGABanner) findComponentById(ResourceTable.Id_banner_main_flip);
        mFlipBanner.setIconYesElement(ResourceTable.Media_yuan_yes);
        mFlipBanner.setIconNoElement(ResourceTable.Media_hui_yuan_no);
        mRotateBanner = (BGABanner) findComponentById(ResourceTable.Id_banner_main_rotate);
        mRotateBanner.setIconYesElement(ResourceTable.Media_circle_select_yes);
        mRotateBanner.setIconNoElement(ResourceTable.Media_circle_select_no);
        mAlphaBanner = (BGABanner) findComponentById(ResourceTable.Id_banner_main_alpha);
        mZoomFadeBanner = (BGABanner) findComponentById(ResourceTable.Id_banner_main_zoomFade);
        mFadeBanner = (BGABanner) findComponentById(ResourceTable.Id_banner_main_fade);
        mZoomCenterBanner = (BGABanner) findComponentById(ResourceTable.Id_banner_main_zoomCenter);
        mZoomCenterBanner.setIconYesElement(ResourceTable.Media_circle_select_yes);
        mZoomCenterBanner.setIconNoElement(ResourceTable.Media_circle_select_no);
        mZoomBanner = (BGABanner) findComponentById(ResourceTable.Id_banner_main_zoom);
        mStackBanner = (BGABanner) findComponentById(ResourceTable.Id_banner_main_stack);
        mZoomStackBanner = (BGABanner) findComponentById(ResourceTable.Id_banner_main_zoomStack);
        mZoomStackBanner.setIconYesElement(ResourceTable.Media_circle_select_yes);
        mZoomStackBanner.setIconNoElement(ResourceTable.Media_circle_select_no);
        mDepthBanner = (BGABanner) findComponentById(ResourceTable.Id_banner_main_depth);
        tv_main_listview_demo = (Text) findComponentById(ResourceTable.Id_tv_main_listview_demo);
        tv_main_listview_demo.setClickedListener(this);
        tv_main_recylerview_demo = (Text) findComponentById(ResourceTable.Id_tv_main_recylerview_demo);
        tv_main_recylerview_demo.setClickedListener(this::onClick);
        tv_main_fresco = (Text) findComponentById(ResourceTable.Id_tv_main_fresco);
        tv_main_fresco.setClickedListener(this::onClick);
        tv_main_visible = (Text) findComponentById(ResourceTable.Id_tv_main_visible);
        tv_main_visible.setClickedListener(this::onClick);
        tv_main_invisible = (Text) findComponentById(ResourceTable.Id_tv_main_invisible);
        tv_main_invisible.setClickedListener(this::onClick);
        tv_main_gone = (Text) findComponentById(ResourceTable.Id_tv_main_gone);
        tv_main_gone.setClickedListener(this::onClick);
        tv_main_change_height = (Text) findComponentById(ResourceTable.Id_tv_main_change_height);
        tv_main_change_height.setClickedListener(this::onClick);
        tv_main_enable_auto_play = (Text) findComponentById(ResourceTable.Id_tv_main_enable_auto_play);
        tv_main_enable_auto_play.setClickedListener(this::onClick);
        tv_main_disable_auto_play = (Text) findComponentById(ResourceTable.Id_tv_main_disable_auto_play);
        tv_main_disable_auto_play.setClickedListener(this::onClick);
        tv_main_start_auto_play = (Text) findComponentById(ResourceTable.Id_tv_main_start_auto_play);
        tv_main_start_auto_play.setClickedListener(this::onClick);
        tv_main_stop_auto_play = (Text) findComponentById(ResourceTable.Id_tv_main_stop_auto_play);
        tv_main_stop_auto_play.setClickedListener(this::onClick);
        tv_main_get_item_count = (Text) findComponentById(ResourceTable.Id_tv_main_get_item_count);
        tv_main_get_item_count.setClickedListener(this::onClick);
        tv_main_get_current_item = findComponentById(ResourceTable.Id_tv_main_get_current_item);
        tv_main_get_current_item.setClickedListener(this::onClick);
        tv_main_load_zero_item = findComponentById(ResourceTable.Id_tv_main_load_zero_item);
        tv_main_load_zero_item.setClickedListener(this::onClick);
        tv_main_load_one_item = findComponentById(ResourceTable.Id_tv_main_load_one_item);
        tv_main_load_one_item.setClickedListener(this::onClick);
        tv_main_load_two_item = findComponentById(ResourceTable.Id_tv_main_load_two_item);
        tv_main_load_two_item.setClickedListener(this::onClick);
        tv_main_load_three_item = findComponentById(ResourceTable.Id_tv_main_load_three_item);
        tv_main_load_three_item.setClickedListener(this::onClick);
        tv_main_load_five_item = findComponentById(ResourceTable.Id_tv_main_load_five_item);
        tv_main_load_five_item.setClickedListener(this::onClick);
        tv_main_select_page_one = findComponentById(ResourceTable.Id_tv_main_select_page_one);
        tv_main_select_page_one.setClickedListener(this::onClick);
        tv_main_select_page_two = findComponentById(ResourceTable.Id_tv_main_select_page_two);
        tv_main_select_page_two.setClickedListener(this::onClick);
        tv_main_select_page_three = findComponentById(ResourceTable.Id_tv_main_select_page_three);
        tv_main_select_page_three.setClickedListener(this::onClick);
        tv_main_select_page_four = findComponentById(ResourceTable.Id_tv_main_select_page_four);
        tv_main_select_page_four.setClickedListener(this::onClick);
        tv_main_select_page_five = findComponentById(ResourceTable.Id_tv_main_select_page_five);
        tv_main_select_page_five.setClickedListener(this::onClick);
        tv_main_cube = findComponentById(ResourceTable.Id_tv_main_cube);
        tv_main_cube.setClickedListener(this::onClick);
        tv_main_depth = findComponentById(ResourceTable.Id_tv_main_depth);
        tv_main_depth.setClickedListener(this::onClick);
        tv_main_flip = findComponentById(ResourceTable.Id_tv_main_flip);
        tv_main_flip.setClickedListener(this::onClick);
        tv_main_rotate = findComponentById(ResourceTable.Id_tv_main_rotate);
        tv_main_rotate.setClickedListener(this::onClick);
        tv_main_alpha = findComponentById(ResourceTable.Id_tv_main_alpha);
        tv_main_alpha.setClickedListener(this::onClick);
        mAdapter = MainAbility.this;
    }

    private void setListener() {
        BGABanner.Delegate mDelegate = MainAbility.this;
        if (mDefaultBanner != null) {
            mDefaultBanner.setDelegate(mDelegate);
            mCubeBanner.setDelegate(mDelegate);
        }
    }

    private void loadData() {
        loadData(mDefaultBanner, 1);
        loadData(mCubeBanner, 2);
        loadData(mAccordionBanner, 3);
        loadData(mFlipBanner, 4);
        loadData(mRotateBanner, 5);
        loadData(mAlphaBanner, 6);
        loadData(mZoomFadeBanner, 3);
        loadData(mFadeBanner, 4);
        loadData(mZoomCenterBanner, 5);
        loadData(mZoomBanner, 6);
        loadData(mStackBanner, 3);
        loadData(mZoomStackBanner, 4);
        loadData(mDepthBanner, 5);
    }

    private void loadData(final BGABanner banner, final int count) {
        /**
         * 设置是否开启自动轮播，需要在 setData 方法之前调用，并且调了该方法后必须再调用一次 setData 方法
         * 例如根据图片当图片数量大于 1 时开启自动轮播，等于 1 时不开启自动轮播
         */
        ArrayList<Integer> list = new ArrayList<>();
        if (count == 1) {
            list.add(ResourceTable.Media_bg2_img);
        } else if (count == 2) {
            list.add(ResourceTable.Media_bg2_img);
            list.add(ResourceTable.Media_bg3_img);
        } else if (count == 3) {
            list.add(ResourceTable.Media_bg4_img);
            list.add(ResourceTable.Media_bg5_img);
            list.add(ResourceTable.Media_bg6_img);
        } else if (count == 5) {
            list.add(ResourceTable.Media_bg7_img);
            list.add(ResourceTable.Media_bg8_img);
            list.add(ResourceTable.Media_bg9_img);
            list.add(ResourceTable.Media_bg10_img);
            list.add(ResourceTable.Media_bg2_img);
        }
        ArrayList<String> tips = new ArrayList<>();
        tips.add("希望");
        banner.setAutoPlayAble(list.size() > 1);
        BGABanner.Adapter mAdapter = MainAbility.this;
        banner.setAdapter(mAdapter);
        banner.setData(list, tips);
    }

    @Override
    public void onBannerItemClick(BGABanner banner, Image itemView, Object model, int position) {
        new ToastDialog(getContext()).setText("点击了第" + (position + 1) + "页").show();
    }

    @Override
    public void fillBannerItem(BGABanner banner, Image itemView, Object model, int position) {
        if (model instanceof String) {
            itemView.setPixelMap(App.getInstance().getBitmap(String.valueOf(model)));
        } else if (model instanceof Integer) {
            itemView.setPixelMap((Integer) model);
        }
    }

    @Override
    public void onClick(Component component) {
        switch (component.getId()) {
            case ResourceTable.Id_tv_main_listview_demo:
                resultData = new Intent();
                operation = new Intent.OperationBuilder()
                        .withDeviceId("")
                        .withBundleName("cn.bingoogolapple.bgabanner.demo")
                        .withAbilityName("cn.bingoogolapple.bgabanner.demo.ui.ListViewDemoAbility")
                        .build();
                resultData.setOperation(operation);
                startAbility(resultData);
                break;
            case ResourceTable.Id_tv_main_recylerview_demo:
                resultData = new Intent();
                operation = new Intent.OperationBuilder()
                        .withDeviceId("")
                        .withBundleName("cn.bingoogolapple.bgabanner.demo")
                        .withAbilityName("cn.bingoogolapple.bgabanner.demo.ui.RecyclerViewsDemoAbility")
                        .build();
                resultData.setOperation(operation);
                startAbility(resultData);
                break;
            case ResourceTable.Id_tv_main_fresco:
                resultData = new Intent();
                operation = new Intent.OperationBuilder()
                        .withDeviceId("")
                        .withBundleName("cn.bingoogolapple.bgabanner.demo")
                        .withAbilityName("cn.bingoogolapple.bgabanner.demo.ui.FrescoDemoAbility")
                        .build();
                resultData.setOperation(operation);
                startAbility(resultData);
                break;
            case ResourceTable.Id_tv_main_visible:
                mDefaultBanner.setVisibility(Component.VISIBLE);
                break;
            case ResourceTable.Id_tv_main_invisible:
                mDefaultBanner.setVisibility(Component.INVISIBLE);
                break;
            case ResourceTable.Id_tv_main_gone:
                mDefaultBanner.setVisibility(Component.HIDE);
                break;
            case ResourceTable.Id_tv_main_change_height:
                mDefaultBanner.setAspectRatio(2.1f);
                break;
            case ResourceTable.Id_tv_main_enable_auto_play:
                mDefaultBanner.setAutoPlayAble(true);
                break;
            case ResourceTable.Id_tv_main_disable_auto_play:
                mDefaultBanner.setAutoPlayAble(false);
                break;
            case ResourceTable.Id_tv_main_start_auto_play:
                mDefaultBanner.startAutoPlay();
                break;
            case ResourceTable.Id_tv_main_stop_auto_play:
                mDefaultBanner.stopAutoPlay();
                break;
            case ResourceTable.Id_tv_main_get_item_count:
                new ToastDialog(this).setText("广告条总页数为" + mDefaultBanner.getItemCount()).show();
                break;
            case ResourceTable.Id_tv_main_get_current_item:
                new ToastDialog(this).setText("广告当前索引位置为" + mDefaultBanner.getCurrentItem()).show();
                break;
            case ResourceTable.Id_tv_main_load_zero_item:
                mDefaultBanner.setAdapter(mAdapter);
                mDefaultBanner.setAutoPlayAble(false);
                mDefaultBanner.setData(null, null);
                mDefaultBanner.setmPlaceholderDrawableResId(ResourceTable.Media_holder);
                mDefaultBanner.showPlaceholder();
                break;
            case ResourceTable.Id_tv_main_load_one_item:
                loadData(mDefaultBanner, 1);
                break;
            case ResourceTable.Id_tv_main_load_two_item:
                loadData(mDefaultBanner, 2);
                break;
            case ResourceTable.Id_tv_main_load_three_item:
                loadData(mDefaultBanner, 3);
                break;
            case ResourceTable.Id_tv_main_load_five_item:
                loadData(mDefaultBanner, 5);
                break;
            case ResourceTable.Id_tv_main_select_page_one:
                mDefaultBanner.setCurrentItem(0);
                break;
            case ResourceTable.Id_tv_main_select_page_two:
                mDefaultBanner.setCurrentItem(1);
                break;
            case ResourceTable.Id_tv_main_select_page_three:
                mDefaultBanner.setCurrentItem(2);
                break;
            case ResourceTable.Id_tv_main_select_page_four:
                mDefaultBanner.setCurrentItem(3);
                break;
            case ResourceTable.Id_tv_main_select_page_five:
                mDefaultBanner.setCurrentItem(4);
                break;
            case ResourceTable.Id_tv_main_cube:
                mDefaultBanner.setTransitionEffect(TransitionEffect.Cube);
                break;
            case ResourceTable.Id_tv_main_depth:
                mDefaultBanner.setTransitionEffect(TransitionEffect.Depth);
                break;
            case ResourceTable.Id_tv_main_flip:
                mDefaultBanner.setTransitionEffect(TransitionEffect.Flip);
                break;
            case ResourceTable.Id_tv_main_rotate:
                mDefaultBanner.setTransitionEffect(TransitionEffect.Rotate);
                break;
            case ResourceTable.Id_tv_main_alpha:
                mDefaultBanner.setTransitionEffect(TransitionEffect.Alpha);
                break;
            default:
                break;
        }
    }
}
